home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / nice.c < prev    next >
C/C++ Source or Header  |  1991-12-07  |  3KB  |  151 lines

  1. /* nice -- run a program with modified scheduling priority
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie <djm@ai.mit.edu> */
  19.  
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. #include <sys/types.h>
  23. #ifndef NICE_PRIORITY
  24. #include <sys/time.h>
  25. #include <sys/resource.h>
  26. #endif
  27. #include "system.h"
  28.  
  29. int isinteger ();
  30. void error ();
  31. void usage ();
  32.  
  33. /* The name this program was run with. */
  34. char *program_name;
  35.  
  36. struct option longopts[] =
  37. {
  38.   {"adjustment", 1, NULL, 'n'},
  39.   {NULL, 0, NULL, 0}
  40. };
  41.  
  42. void
  43. main (argc, argv)
  44.      int argc;
  45.      char **argv;
  46. {
  47.   int current_priority;
  48.   int adjustment = 0;
  49.   int minusflag = 0;
  50.   int adjustment_given = 0;
  51.   int optc;
  52.  
  53.   program_name = argv[0];
  54.  
  55.   while ((optc = getopt_long (argc, argv, "+0123456789-n:", longopts,
  56.                   (int *) 0)) != EOF)
  57.     {
  58.       switch (optc)
  59.     {
  60.     case '?':
  61.       usage ();
  62.       
  63.     case 'n':
  64.       if (!isinteger (optarg))
  65.         error (1, 0, "invalid priority `%s'", optarg);
  66.       adjustment = atoi (optarg);
  67.       adjustment_given = 1;
  68.       break;
  69.  
  70.     case '-':
  71.       minusflag = 1;
  72.       break;
  73.  
  74.     default:
  75.       adjustment = adjustment * 10 + optc - '0';
  76.       adjustment_given = 1;
  77.     }
  78.     }
  79.  
  80.   if (minusflag)
  81.     adjustment = -adjustment;
  82.   if (!adjustment_given)
  83.     adjustment = 10;
  84.  
  85.   if (optind == argc)
  86.     {
  87.       if (adjustment_given)
  88.     usage ();
  89.       /* No command given; print the priority. */
  90.       errno = 0;
  91. #ifndef NICE_PRIORITY
  92.       current_priority = getpriority (PRIO_PROCESS, 0);
  93. #else
  94.       current_priority = nice (0);
  95. #endif
  96.       if (current_priority == -1 && errno != 0)
  97.     error (1, errno, "cannot get priority");
  98.       printf ("%d\n", current_priority);
  99.       exit (0);
  100.     }
  101.  
  102.   errno = 0;
  103. #ifndef NICE_PRIORITY
  104.   current_priority = getpriority (PRIO_PROCESS, 0);
  105. #else
  106.   current_priority = nice (0);
  107. #endif
  108.   if (current_priority == -1 && errno != 0)
  109.     error (1, errno, "cannot get priority");
  110.  
  111. #ifndef NICE_PRIORITY
  112.   if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
  113. #else
  114.   if (nice (adjustment) == -1)
  115. #endif
  116.     error (1, errno, "cannot set priority");
  117.  
  118.   execvp (argv[optind], &argv[optind]);
  119.   error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
  120. }
  121.  
  122. /* Return nonzero if S represents a (possibly signed) decimal integer,
  123.    zero if not. */
  124.  
  125. int
  126. isinteger (s)
  127.      char *s;
  128. {
  129.   if (*s == '-')
  130.     ++s;
  131.   if (*s == 0)
  132.     return 0;
  133.   while (*s)
  134.     {
  135.       if (*s < '0' || *s > '9')
  136.     return 0;
  137.       ++s;
  138.     }
  139.   return 1;
  140. }
  141.  
  142. void
  143. usage ()
  144. {
  145.   fprintf (stderr, "\
  146. Usage: %s [-n adjustment] [-adjustment] [--adjustment=adjustment]\n\
  147.        [command [arg...]]\n",
  148.        program_name);
  149.   exit (1);
  150. }
  151.